<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;

/**
 * 帖子模型
 */
class ForumBbs extends ModelBase
{
    protected $insert = ['create_time'];


    public function getImagesAttr(){
        if($this->img_ids){
            $img_arr=explode(',',$this->img_ids);
            $arr=[];
            foreach ($img_arr as $k=>$v){
                $arr[]= get_picture_url($v,false);
            }
            return $arr;
        }else{
            return [];
        }

    }


    /**
     * 连分类表
     */
    public function cate(){
        return $this->belongsTo('ForumBbsCategory','cate_id','id');
    }

    /**
     * 连留言表
     */
    public function reply(){
        return $this->hasMany('ForumBbsComment','bbs_id','id');
    }

    /**
     *是否点赞
     */
    public function zan($uid)
    {

        if(ForumBbsFabulous::where(['user_id'=>$uid,'bbs_id'=>$this->id])->find()){
            return 1;
        }else{
            return 0;
        }
    }
    /**
     * 获取状态信息
     */
    public function getStateTextAttr(){
        $arr=['待审核','审核通过','审核不通过'];
        return lang($arr[$this->state]);
    }

    /**
     *是否关注
     */
    public function follow($uid)
    {

        if(ForumFollow::where(['user_id'=>$uid,'uid'=>$this->user_id,'status'=>1])->find()){
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 回帖数量
     */
    public function getReplyCountAttr(){
       return  count($this->reply);
    }

    /**
     * 回帖数量
     */
    public function endreply($uid){

       if( count($this->reply)>0){

           $where1=['user_id'=>$uid,'uid'=>$uid,'is_hide'=>0];
           return ForumBbsComment::where(['bbs_id'=>$this->id])->where(function ($q)use($where1){
               $q->whereOr($where1);
           })->order('id desc')->find();
       }else{
           return  [];
       }

    }

    /**
     * 标签数组
     */
    public function getTagsArrayAttr(){
        if($this->tags){
            $tags=rtrim($this->tags, ",");
            $tags=ltrim($tags, ",");
            return explode(',',$tags);
        }else{
            return [];
        }

    }

    public function  getNameAttr($value){

        if( isset($_REQUEST['lang']) && $_REQUEST['lang']!='zh-cn' &&  $_REQUEST['lang'] ){
            return   baidu_translate($value);
        }
        return $value;

    }

    /**
     * 连用户表
     */
    public function user(){
        return $this->belongsTo('Member','user_id','id');
    }

    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }
  
  public function  getCateNameAttr(){

        if( isset($_REQUEST['lang']) && $_REQUEST['lang']!='zh-cn' &&  $_REQUEST['lang'] ){
         return   baidu_translate($this->cate['name']);
        }
        return $this->cate['name'];

    }



    public function  getContentAttr($value){
        if(isset($_REQUEST['lang']) && $_REQUEST['lang']!='zh-cn' &&  $_REQUEST['lang'] ){
            return   baidu_translate($value);
        }
        return $value;

    }
}
